<?php
namespace app\index\controller;

use app\BaseController;
use think\facade\Db;
use think\facade\Request;

class Xgj extends Base
{
    public function index(){
    $login =session('userAccount'); 
    
    // $namecodeData=Db::name('xcxrenzheng')->where('uid',$login['id'])->select();
  
             return view('',[
    //   'namecodeData'=>$namecodeData,
       'login'=>$login,
       'loginsf'=>$login['username'],
        ]);
    }
 public function xcxurladd(){
    $login =session('userAccount'); 
     if(!$login) {
        header('Location: /index/login');
    //   return $this->redirect('/index/login');
    }
    	if(request()->isPost()) {
			$data = input('post.');
			$data['uid']=$login['id'];
			$data['status']='1';
			$data['time']=time();
			$res = Db::name('xcxurl')->insertGetId($data);
		
			$urls1='http://xcx.zevichen.me/xgj/goxcxurl?id='.$res;
			$urls2='https://ock.cn/api/short?token=62567328d5338&longurl='.$urls1;
            $html = file_get_contents($urls2);
            $urls3=json_decode($html, true);
            $url['url']=$urls3['data']['short'];
			$ress=Db::name('xcxurl')->where('id',$res)->update($url);
			if($res){
				return alert('操作成功！','/index/xgj/xcxurl',6,3);
			 }else{
				return alert('操作失败！','/index/xgj/xcxurl',5,3);
			 }

		}
        return view('',[
    //   'namecodeData'=>$namecodeData,
       'login'=>$login,
       'loginsf'=>$login['username'],
        ]);  
  
    // $namecodeData=Db::name('xcxrenzheng')->where('uid',$login['id'])->select();
  
    }

    public function xcxurl(){
    $login =session('userAccount'); 
     if(!$login) {
          
    $xcxurl=NULL;
        return view('',[
    //   'namecodeData'=>$namecodeData,
       'xcxurl'=>$xcxurl,
       'login'=>$login,
       'loginsf'=>$login['username'],
        ]);
    }else {
        $uid=$login['id'];
        $xcxurl=Db::name('xcxurl')->where('uid',$uid)->select();
   
    
        return view('',[
    //   'namecodeData'=>$namecodeData,
       'xcxurl'=>$xcxurl,
       'login'=>$login,
       'loginsf'=>$login['username'],
        ]);
      
    }
    // $namecodeData=Db::name('xcxrenzheng')->where('uid',$login['id'])->select();
  
         
    }
    public function goxcxurl()
    {
     $id=input('id');
     $xcxurl=Db::name('xcxurl')->where('status','>=',1)->where('id',$id)->find();   
     $appid=$xcxurl['appid'];
     $secret=$xcxurl['secret'];
     $url="https://api.weixin.qq.com/cgi-bin/token?grant_type=client_credential&appid=".$appid ."&secret=".$secret;
     $html = file_get_contents($url);
     $res=json_decode($html, true);
   
    $url = 'https://api.weixin.qq.com/wxa/generatescheme?access_token='.$res['access_token'];
    $method = 'POST';
    $bodys = array(
     
    );
    // 配置headers 
    $headers[] = "Accept:*/*";
    $headers[] = "Accept-Language:zh-CN,zh;q=0.8";
    $headers[] = "Connection:close";

    // 创建连接
    $curl = curl_init($url);
    curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($curl, CURLOPT_CUSTOMREQUEST, $method);
    curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
    curl_setopt($curl, CURLOPT_FAILONERROR, false);
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($curl, CURLOPT_HEADER, false);
    curl_setopt($curl, CURLOPT_POST, true);
    curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($bodys));
    
    // 发送请求
    $response = curl_exec($curl);
    curl_close($curl);
     $ress=json_decode($response, true);
    // 读取响应
  
  
        return view('',[
   'openlink'=>$ress['openlink'],
    'xcxurl'=> $xcxurl,
  
        ]);
    }
  
}
